package com.gzhryc.shared_device.cdcz.core.protocol.v1.models;

import com.gzhryc.common.NumberTools;
import com.gzhryc.common.StringTools;
import com.gzhryc.shared_device.cdcz.core.protocol.v1.V1_CDCZConstants;

import java.nio.charset.StandardCharsets;

public class BKVData {

    private byte[] data;
    private int dataLength;
    private int keyLength;
    private String key;
    private byte[] value;

    public BKVData(){}

    public BKVData(String key,byte[] value) {
        this.key = key;
        this.value = value;
        this.buildData();
    }

    public BKVData(String key,String value) {
        this.key = key;
        this.value = StringTools.hexStr2ByteArr(value);
        this.buildData();
    }

    public BKVData(String key,String value,Integer valueType) {
        this.key = key;
        if(V1_CDCZConstants.VALUE_TYPE_STRING.equals(valueType)) {
            this.value = value.getBytes(StandardCharsets.UTF_8);
        }else {
            this.value = StringTools.hexStr2ByteArr(value);
        }
        this.buildData();
    }

    public BKVData(String key,Integer value) {
        this.key = key;
        String hex = Integer.toHexString(value);
        //补位
        if(hex.length() % 2 == 1){
            hex = "0" + hex;
        }
        this.value = StringTools.hexStr2ByteArr(hex);
        this.buildData();
    }

    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        this.data = data;
    }

    public int getDataLength() {
        return dataLength;
    }

    public void setDataLength(int dataLength) {
        this.dataLength = dataLength;
    }

    public int getKeyLength() {
        return keyLength;
    }

    public void setKeyLength(int keyLength) {
        this.keyLength = keyLength;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public byte[] getValue() {
        return value;
    }

    public void setValue(byte[] value) {
        this.value = value;
    }

    public String getHexValue(){
        if(this.value != null && this.value.length > 0){
            return StringTools.byteArr2HexStr(this.value);
        }
        return null;
    }

    public String getStringValue(){
        if(this.value != null && this.value.length > 0){
            return  new String(this.value, StandardCharsets.UTF_8);
        }
        return null;
    }

    public Integer getIntegerValue(){
        if(this.value != null && this.value.length > 0){
            return  Integer.valueOf(StringTools.byteArr2HexStr(this.value),16);
        }
        return null;
    }

    public Double getDoubleValue(Integer decimal) {
        if (this.value != null && this.value.length > 0) {
            Integer temp = Integer.valueOf(StringTools.byteArr2HexStr(this.value), 16);
            if (decimal == 0) {
                return Double.valueOf(temp.toString());
            } else if (decimal == 1) {
                return NumberTools.round(temp / 10.0, 1);
            } else if (decimal == 2) {
                return NumberTools.round(temp / 100.0, 2);
            } else if (decimal == 3) {
                return NumberTools.round(temp / 1000.0, 3);
            }
        }
        return null;
    }

    private void buildData() {
        byte[] keyBytes = StringTools.hexStr2ByteArr(this.key);
        this.keyLength = keyBytes.length;
        this.dataLength = keyLength + value.length + 1; //Key长度，Key值，值域

        this.data = new byte[(this.dataLength + 1)];
        this.data[0] = StringTools.hexStr2ByteArr(Integer.toHexString(this.dataLength))[0];
        this.data[1] = StringTools.hexStr2ByteArr(Integer.toHexString(keyBytes.length))[0];
        int num = 2;
        for (int i = 0; i < keyBytes.length; i++) {
            this.data[(i + num)] = keyBytes[i];
        }
        num = num + keyBytes.length;
        for (int i = 0; i < this.value.length; i++) {
            this.data[(i + num)] = this.value[i];
        }
    }
}
